package spotGUI.eventsGUI;

import java.awt.Component;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.DateFormat;
import java.util.Calendar;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;

import managers.DBManager;
import managers.EventsManager;
import managers.socialNetworkManagers.SocialNetworkManager;
import spotGUI.utils.SocialUserSelectionListener;
import utils.SocialUserData;
import utils.StackTracePrinter;
import elements.Event;
import java.util.ResourceBundle;

public class EventViewer extends JPanel {



	private JLabel lblTitle;
	private JLabel lblParticipants;
	private JLabel lblWhen;
	private JLabel lblDate;
	private JLabel lblImage;
	private JTextArea textAreaBody;

	private Event event;
	private JButton btnParticipant;
	private JList listParticipants;
	private JLabel lblWho;
	private Font font = new Font("Dialog", Font.BOLD, 16);
	private JLabel lblTarget;
	private Font font2 = new Font("Dialog", Font.PLAIN, 16);

	private EventsManager manager;
	private JScrollPane scrollPane;
	private JScrollPane scrollPane_1;

	public EventViewer(final EventsManager manager) {

		this.manager = manager;

		setBounds(100, 100, 761, 605);
		setBorder(new EmptyBorder(10, 10, 10, 10));
		GridBagLayout gbl_contentPanel = new GridBagLayout();
		gbl_contentPanel.columnWidths = new int[]{78, 80, 14, 197};
		gbl_contentPanel.rowHeights = new int[]{33, 22, 22, 0, 0, 0};
		gbl_contentPanel.columnWeights = new double[]{0.0, 0.0, 1.0, 0.0};
		gbl_contentPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		setLayout(gbl_contentPanel);

		lblTitle = new JLabel("TitoloEvento");
		lblTitle.setFont(new Font("Agency FB", Font.BOLD, 28));
		GridBagConstraints gbc_lblTitle = new GridBagConstraints();
		gbc_lblTitle.gridwidth = 3;
		gbc_lblTitle.anchor = GridBagConstraints.SOUTHWEST;
		gbc_lblTitle.insets = new Insets(0, 0, 5, 5);
		gbc_lblTitle.gridx = 0;
		gbc_lblTitle.gridy = 0;
		add(lblTitle, gbc_lblTitle);

		lblWhen = new JLabel(ResourceBundle.getBundle("spotGUI.eventsGUI.messages").getString("EventViewer.lblWhen.text")); //$NON-NLS-1$ //$NON-NLS-2$
		lblWhen.setFont(font);
		GridBagConstraints gbc_lblWhen = new GridBagConstraints();
		gbc_lblWhen.anchor = GridBagConstraints.SOUTHEAST;
		gbc_lblWhen.insets = new Insets(0, 0, 5, 5);
		gbc_lblWhen.gridx = 0;
		gbc_lblWhen.gridy = 1;
		add(lblWhen, gbc_lblWhen);

		lblDate = new JLabel("");
		lblDate.setFont(font2);
		GridBagConstraints gbc_lblDate = new GridBagConstraints();
		gbc_lblDate.anchor = GridBagConstraints.SOUTHWEST;
		gbc_lblDate.insets = new Insets(0, 0, 5, 5);
		gbc_lblDate.gridx = 1;
		gbc_lblDate.gridy = 1;
		add(lblDate, gbc_lblDate);

		lblImage = new JLabel("");
		lblImage.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
		GridBagConstraints gbc_lblImage = new GridBagConstraints();
		gbc_lblImage.gridheight = 3;
		gbc_lblImage.anchor = GridBagConstraints.NORTHEAST;
		gbc_lblImage.insets = new Insets(0, 0, 5, 0);
		gbc_lblImage.gridx = 3;
		gbc_lblImage.gridy = 0;
		add(lblImage, gbc_lblImage);

		lblWho = new JLabel(ResourceBundle.getBundle("spotGUI.eventsGUI.messages").getString("EventViewer.lblWho.text")); //$NON-NLS-1$ //$NON-NLS-2$
		lblWho.setFont(font);
		GridBagConstraints gbc_lblWho = new GridBagConstraints();
		gbc_lblWho.anchor = GridBagConstraints.NORTHEAST;
		gbc_lblWho.insets = new Insets(0, 0, 5, 5);
		gbc_lblWho.gridx = 0;
		gbc_lblWho.gridy = 2;
		add(lblWho, gbc_lblWho);

		lblTarget = new JLabel("");
		lblTarget.setFont(font2);
		GridBagConstraints gbc_lblTarget = new GridBagConstraints();
		gbc_lblTarget.anchor = GridBagConstraints.NORTHWEST;
		gbc_lblTarget.insets = new Insets(0, 0, 5, 5);
		gbc_lblTarget.gridx = 1;
		gbc_lblTarget.gridy = 2;
		add(lblTarget, gbc_lblTarget);

		btnParticipant = new JButton(ResourceBundle.getBundle("spotGUI.eventsGUI.messages").getString("EventViewer.btnParticipant.text")); //$NON-NLS-1$ //$NON-NLS-2$
		btnParticipant.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					manager.setParticipant(manager.getUserId(), event.eventId, DBManager.getConnection());
					((DefaultListModel)listParticipants.getModel()).addElement(SocialNetworkManager.getSharedInstance().getUser(manager.getUserId()));
					btnParticipant.setEnabled(false);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					StackTracePrinter.print(e);
				}
			}
		});
		GridBagConstraints gbc_btnParticipant = new GridBagConstraints();
		gbc_btnParticipant.anchor = GridBagConstraints.WEST;
		gbc_btnParticipant.insets = new Insets(0, 0, 5, 5);
		gbc_btnParticipant.gridx = 0;
		gbc_btnParticipant.gridy = 3;
		add(btnParticipant, gbc_btnParticipant);

		lblParticipants = new JLabel(ResourceBundle.getBundle("spotGUI.eventsGUI.messages").getString("EventViewer.lblParticipants.text")); //$NON-NLS-1$ //$NON-NLS-2$
		lblParticipants.setHorizontalAlignment(SwingConstants.RIGHT);
		lblParticipants.setFont(new Font("Agency FB", Font.BOLD | Font.ITALIC, 22));
		GridBagConstraints gbc_lblParticipants = new GridBagConstraints();
		gbc_lblParticipants.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblParticipants.insets = new Insets(0, 0, 5, 0);
		gbc_lblParticipants.gridx = 3;
		gbc_lblParticipants.gridy = 3;
		add(lblParticipants, gbc_lblParticipants);

		scrollPane = new JScrollPane();
		scrollPane.setBorder(null);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.gridwidth = 3;
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.insets = new Insets(0, 0, 0, 5);
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 4;
		add(scrollPane, gbc_scrollPane);

		textAreaBody = new JTextArea();
		textAreaBody.setBorder(null);
		textAreaBody.setOpaque(false);
		scrollPane.setViewportView(textAreaBody);
		textAreaBody.setFont(new Font("Dialog", Font.PLAIN, 14));
		textAreaBody.setWrapStyleWord(true);
		textAreaBody.setEditable(false);
		textAreaBody.setLineWrap(true);

		scrollPane_1 = new JScrollPane();
		scrollPane_1.setBorder(null);
		GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
		gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
		gbc_scrollPane_1.gridx = 3;
		gbc_scrollPane_1.gridy = 4;
		add(scrollPane_1, gbc_scrollPane_1);

		listParticipants = new JList();
		listParticipants.setBackground(UIManager.getColor("Desktop.background"));
		scrollPane_1.setViewportView(listParticipants);
		listParticipants.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		listParticipants.setFont(new Font("Dialog", Font.BOLD, 12));
		listParticipants.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listParticipants.addListSelectionListener(new SocialUserSelectionListener());
		listParticipants.setModel(new DefaultListModel());
		listParticipants.setCellRenderer(new SocialUserListCellRenderer());

	}

	public void setEvent (Event event) {
		this.event = event;
		listParticipants.setVisible(true);
		lblParticipants.setVisible(true);
		lblTitle.setText(event.name);
		lblImage.setIcon(event.photo);
		lblDate.setText(DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT).format(event.date));
		lblTarget.setText(event.target);
		textAreaBody.setText(event.body);
		((DefaultListModel)listParticipants.getModel()).removeAllElements();
		if (event.participants != null) {
			for (int i = 0; i < event.participants.size(); i++) {
				((DefaultListModel)listParticipants.getModel()).addElement(SocialNetworkManager.getSharedInstance().getUser(event.participants.get(i)));
			}
			if (((DefaultListModel)listParticipants.getModel()).contains(SocialNetworkManager.getSharedInstance().getUser(manager.getUserId()))) {
				btnParticipant.setEnabled(false);
			} else {
				btnParticipant.setEnabled(true);
			}
		}
		if (event.date.before(Calendar.getInstance().getTime()))
			btnParticipant.setEnabled(false);
	}

	public void setNoEvents (Event noEvents) {
		setEvent(noEvents);
		lblDate.setText("//");
		lblTarget.setText("//");
		btnParticipant.setEnabled(false);
		listParticipants.setVisible(false);
		lblParticipants.setVisible(false);
	}

	private class SocialUserListCellRenderer implements ListCellRenderer {

		@Override
		public Component getListCellRendererComponent(JList list, Object value,
				int index, boolean isSelected, boolean cellHasFocus) {

			return new SocialRendererComponent((SocialUserData)value);
		}


		private class SocialRendererComponent extends JPanel {

			private JLabel lblName;
			private JLabel lblPhoto;


			public SocialRendererComponent (SocialUserData user) {
				setToolTipText(user.getPersonalMessage());

				GridBagLayout gridBagLayout = new GridBagLayout();
				gridBagLayout.columnWidths = new int[]{20, 67, 0};
				gridBagLayout.rowHeights = new int[]{7, 0};
				gridBagLayout.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
				gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
				setLayout(gridBagLayout);

				lblPhoto = new JLabel("");
				lblPhoto.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
				lblPhoto.setIcon(user.getPhoto());
				GridBagConstraints gbc_lblPhoto = new GridBagConstraints();
				gbc_lblPhoto.insets = new Insets(0, 0, 0, 5);
				gbc_lblPhoto.gridx = 0;
				gbc_lblPhoto.gridy = 0;
				add(lblPhoto, gbc_lblPhoto);

				lblName = new JLabel(user.getFirstName()+" "+user.getSurname());
				GridBagConstraints gbc_name = new GridBagConstraints();
				gbc_name.anchor = GridBagConstraints.WEST;
				gbc_name.insets = new Insets(0, 0, 0, 5);
				gbc_name.gridx = 1;
				gbc_name.gridy = 0;
				add(lblName, gbc_name);
			}

		}
	}

}
